<template>
	<div>
		<el-card shadow="hover">
			<div class="mb15">
				<el-row :gutter="10" class="mb8" v-if="permissionStore.isPermissionsBtn('012002001')">
					<el-col :span="1.5">
						<el-button type="primary" @click="handleAdd"><el-icon>
								<Plus />
							</el-icon>新增</el-button>
					</el-col>
				</el-row>
			</div>
			<el-table v-loading="loading" :data="tableData.data">
				<el-table-column label="图片" align="center" min-width="100px">
					<template #default="{ row }">
						<el-image :src="row.img" style="width: 50px;height: 100px;" preview-teleported
							:preview-src-list="[row.img]" />
					</template>
				</el-table-column>
				<el-table-column label="访问地址" align="center" prop="url" min-width="100px"/>
				<el-table-column label="状态" align="center"  min-width="100px">
					<template #default="{ row }">
						<el-tag :type=" row.states==1?'success':'danger'">{{ row.states==1?'启用':'停用' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="开始时间" align="center" prop="beginTime" :formatter="formatDateTime"	min-width="150px" />
				<el-table-column label="截止时间" align="center" prop="endTime" :formatter="formatDateTime"	min-width="150px" />
				<el-table-column label="更新时间" align="center" prop="updateTime" :formatter="formatDateTime"	min-width="150px" />

				<el-table-column label="操作" align="center" v-if="permissionStore.isPermissionsBtn(['012002002','012002003'])"
					class-name="small-padding" min-width="150px" fixed="right">
					<template #default="scope">
						<el-button  v-if="permissionStore.isPermissionsBtn('012002002')" text type="primary"
							@click="handleUpdate(scope.row)"><el-icon>
								<EditPen />
							</el-icon>修改</el-button>
						<el-button  v-if="permissionStore.isPermissionsBtn('012002003')" text type="danger"
							@click="handleDel(scope.row)"><el-icon>
								<Delete />
							</el-icon>删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNumber"
				v-model:limit="tableData.param.pageLength" @pagination="getList" />
		</el-card>
		<addView ref="addRef" @getList="getList"></addView>
		<editView ref="editRef" @getList="getList"></editView>
	</div>
</template>
<script lang="ts">
import { launchListApi, delLaunchApi } from '@/api/operationsManage/advert';
import { toRefs, reactive, onMounted, ref, defineComponent, getCurrentInstance, toRaw } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { usePermissionStore } from "@/store/modules/permission"
import editView from './component/edit.vue';
import addView from './component/add.vue';
import {  formatDateTime } from '@/utils/index'
export default defineComponent({
	name: 'advertList', //开屏广告列表
	components: {
		editView,
		addView
	},
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const queryRef = ref();
		const addRef = ref()
		const editRef = ref();
		const permissionStore = usePermissionStore()
		const state = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					pageNumber: undefined,
					pageLength: undefined,
				},
			},
		});

		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;
			launchListApi(state.tableData.param).then((res: any) => {
				let list = res.data.list ?? [];
				state.tableData.data = list
				state.tableData.total = res.data.total;
				loading.value = false;
			});
		};
		const handleAdd = () => {
			addRef.value.openDialog()
		}
		const handleUpdate = (row) => {
			const rawRow = structuredClone(toRaw(row));
			editRef.value.openDialog(rawRow);
		};
		const handleDel = (row) => {

			let msg = `此操作将永久删除，是否继续?`
			ElMessageBox.confirm(msg, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					delLaunchApi({id:row.id}).then(() => {
						ElMessage.success('删除成功');
						getList();
					});
				})
				.catch(() => {});
			
		}
		return {
			proxy,
			editRef,
			loading,
			queryRef,
			addRef,
			permissionStore,
			getList,
			handleUpdate,
			handleAdd,
			formatDateTime,
			handleDel,
			...toRefs(state),
		};
	},
});
</script>
<style lang="scss" scoped>
.colBlock {
	display: block;
}

.colNone {
	display: none;
}
</style>
